/**
 * @author hello
 */

$(document).ready(function(){
    $('#login').click(function(){
    	var email = trim($('#email').val()),
    		password = trim($('#password').val()),
    		role = trim($('#role').val());
    	
    		$('#email_help').html('');
        	$('#password_help').html('');
        	
        	if(!checkEmail(email))
        		$('#email_help').html('email wrong');
        	else if(!checkPassword(password))
        		$('#password_help').html('password wrong');
        	else{
        		$.ajax({
					type:'POST',
					url: 'login',
					dataType: "json",
					data: {"email":email,"password":password,"role":role},			
					success:function(json){
							if(json.success != 1){
								$('#email_help').html('eithor email or password wrong!');
							}else{
								$('#login_form').submit();
							}
					}
        		});
        	}
    })
    
    function checkEmail(email){
    	if(email && /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email))
    		return true;
    	else
    		return false;
    }
    function checkPassword(password){
    	if(password && checkLength(password,6,10))
    		return true;
    	else
    		return false;
    }
    function trim(str){
    	if(str != null)
 　　           	return str.replace(/(^\s*)|(\s*$)/g, "");
    	else 
    		return str;
 　    }
    function checkLength(o,min,max){
    	if(o.length > max || o.length < min){
    		return false;
    	}else{
    		return true;
    	}
    }
    
    $('#about,#contact').click(function(){
    	$('#about_form,#contact_form').submit();
    })
})
